package com.jiangaifeng.blog.service.impl;

import java.util.List;
import com.jiangaifeng.common.utils.DateUtils;
import com.jiangaifeng.common.utils.file.FileUploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jiangaifeng.blog.mapper.BlogPostMapper;
import com.jiangaifeng.blog.domain.BlogPost;
import com.jiangaifeng.blog.service.IBlogPostService;

/**
 * postService业务层处理
 * 
 * @author bug姜
 * @date 2024-12-26
 */
@Service
public class BlogPostServiceImpl implements IBlogPostService 
{
    @Autowired
    private BlogPostMapper blogPostMapper;

    /**
     * 查询post
     * 
     * @param id post主键
     * @return post
     */
    @Override
    public BlogPost selectBlogPostById(Long id)
    {
        return blogPostMapper.selectBlogPostById(id);
    }

    /**
     * 查询post列表
     * 
     * @param blogPost post
     * @return post
     */
    @Override
    public List<BlogPost> selectBlogPostList(BlogPost blogPost)
    {
        return blogPostMapper.selectBlogPostList(blogPost);
    }

    /**
     * 新增post
     * 
     * @param blogPost post
     * @return 结果
     */
    @Override
    public int insertBlogPost(BlogPost blogPost)
    {
        blogPost.setCreateTime(DateUtils.getNowDate());
        blogPost.setUpdateTime(DateUtils.getNowDate());
        blogPost.setStatus(0L);
        return blogPostMapper.insertBlogPost(blogPost);
    }

    /**
     * 修改post
     * 
     * @param blogPost post
     * @return 结果
     */
    @Override
    public int updateBlogPost(BlogPost blogPost)
    {
        blogPost.setUpdateTime(DateUtils.getNowDate());
        return blogPostMapper.updateBlogPost(blogPost);
    }

    /**
     * 批量删除post
     * 
     * @param ids 需要删除的post主键
     * @return 结果
     */
    @Override
    public int deleteBlogPostByIds(Long[] ids)
    {
        for( Long id : ids ){
            BlogPost post = blogPostMapper.selectBlogPostById( id );
            FileUploadUtils.deleteMinio( post.getPath() );
        }
        return blogPostMapper.deleteBlogPostByIds(ids);
    }

    /**
     * 删除post信息
     * 
     * @param id post主键
     * @return 结果
     */
    @Override
    public int deleteBlogPostById(Long id)
    {
        //删除minio附件
        BlogPost post = blogPostMapper.selectBlogPostById( id );
        FileUploadUtils.deleteMinio( post.getPath() );
        return blogPostMapper.deleteBlogPostById(id);
    }
}
